import axios from "axios";
//@ts-ignore
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const instance = axios.create({
    baseURL: '/api',//请求基础地址
    timeout: 5000,
    withCredentials: true
})

// 发起请求之前执行
instance.interceptors.request.use(function (config) {
    //@ts-ignore
    config.headers.Authorization= sessionStorage.getItem('token')
    Nprogress.start()//启动loading
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// 请求返回之后执行
instance.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    Nprogress.done()
    return response;
}, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    Nprogress.done()//关闭loading
    return Promise.reject(error);
});
/**
 * get请求地址
 * @param url 地址
 * @param params 参数
 */
export const get = (url, params={}) => instance.get(url, {params}).then((res)=>res.data)
/**
 *
 * @param url
 * @param data
 */
export const post = (url, data={}) => instance.post(url, data).then((res)=>res.data)
/**
 *
 * @param url
 * @param data
 */
export const put = (url, data={}) => instance.put(url, data).then((res)=>res.data)
/**
 *
 * @param url
 * @param data
 */
export const patch = (url, data={}) => instance.patch(url, data).then((res)=>res.data)
/**
 *
 * @param url
 */
export const del=(url)=>instance.delete(url).then((res)=>res.data)

